//
//  XS_SetVC.swift
//  XueDao
//
//  Created by fandongtongxue on 2020/12/10.
//

import UIKit

class XS_SetVC: XS_BaseVC {

    @IBOutlet weak var tableView: UITableView!
    var model : XS_UserBOM?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        title = "设置"
        tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        tableView.register(UINib.init(nibName: "XS_SetListCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_SetListCell")
        tableView.delegate = self
        tableView.dataSource = self
    }

    @IBAction func logoutBtnAction(_ sender: UIButton) {
        //do logout
        let backActionVC = UIAlertController.init(title: "是否确定退出？", message: "", preferredStyle: .alert)
        let cancelAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        cancelAction.setValue(UIColor.lightGray, forKey: "titleTextColor")
        let sureAction = UIAlertAction.init(title: "退出", style: .destructive) { (action) in
            XS_DownloadManager.shared.removeAllFile()
            XS_NVC.shared.toLogin(true)
        }
        sureAction.setValue(UIColor.red, forKey: "titleTextColor")
        backActionVC.addAction(cancelAction)
        backActionVC.addAction(sureAction)
        
        self.present(backActionVC, animated: true, completion: nil)
    }
    
    @objc func cellarSwitchAction(cellarSwitch: UISwitch){
        //开启关闭流量看视频
        UserDefaults.standard.setValue(cellarSwitch.isOn, forKey: .xsKey_videoAllowCellar)
        UserDefaults.standard.synchronize()
    }
}

extension XS_SetVC : UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 1
        case 2:
//            return 4
            return 3
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XS_SetListCell", for: indexPath) as! XS_SetListCell
        switch indexPath.section {
        case 0:
            cell.nextImageView.isHidden = false
            if indexPath.row == 1 {
                cell.titleLabel.text = "个人信息"
                cell.rightNextLabel.text = XS_Singleton.shared.infoModel?.userBO.username
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
            }else{
                cell.titleLabel.text = "账号与安全"
                cell.rightNextLabel.text = ""
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
            }
            break
        case 1:
            cell.nextImageView.isHidden = true
            cell.titleLabel.text = "允许3G/4G网络播放视频"
            cell.rightNextLabel.text = ""
            cell.rightLabel.text = ""
            cell.cellarSwitch.isHidden = false
            let value = UserDefaults.standard.value(forKey: .xsKey_videoAllowCellar)
            if value != nil {
                let boolValue = value as! Bool
                cell.cellarSwitch.setOn(boolValue, animated: true)
            }else{
                cell.cellarSwitch.setOn(true, animated: true)
            }
            cell.cellarSwitch.addTarget(self, action: #selector(cellarSwitchAction), for: .valueChanged)
            break
        case 2:
            switch indexPath.row {
            case 0:
                cell.nextImageView.isHidden = true
                cell.titleLabel.text = "清除缓存"
                let size = SDWebImageManager.shared().imageCache?.getSize() ?? 0
                cell.rightLabel.text = String.init(format: "%ldMB", size/1024/1024)
                cell.cellarSwitch.isHidden = true
                cell.rightNextLabel.text = ""
                break
//            case 1:
//                cell.nextImageView.isHidden = false
//                cell.titleLabel.text = "隐私设置"
//                cell.rightLabel.text = ""
//                cell.cellarSwitch.isHidden = true
//                cell.rightNextLabel.text = ""
//                break
//            case 2:
            case 1:
                cell.nextImageView.isHidden = false
//                cell.titleLabel.text = "报告问题"
                cell.titleLabel.text = "意见反馈"
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightNextLabel.text = ""
                break
//            case 3:
            case 2:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "关于"
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightNextLabel.text = ""
                break
            default:
                break
            }
            break
        default:
            break
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: .screenWidth, height: 8))
        view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F7")
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 || section == 2{
            return 8
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            if indexPath.row == 1 {
                //去个人信息
                let infoVC = XS_MineInfoVC()
                navigationController?.pushViewController(infoVC, animated: true)
            }else{
                //去账号与安全
                let accountSafeVC = XS_AccountSafeVC()
                accountSafeVC.model = model
                navigationController?.pushViewController(accountSafeVC, animated: true)
            }
            break
        case 2:
            switch indexPath.row {
            case 0:
                //清除缓存
                SDWebImageManager.shared().imageCache?.clearDisk(onCompletion: {
                    self.view.makeToast("清除成功")
                    let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 2)) as! XS_SetListCell
                    let size = SDWebImageManager.shared().imageCache?.getSize() ?? 0
                    cell.rightLabel.text = String.init(format: "%ldMB", size/1024/1024)
                })
            break
//            case 1:
//                //隐私设置
//                let privacySetVC = XS_PrivacySetVC()
//                navigationController?.pushViewController(privacySetVC, animated: true)
//            break
//            case 2:
            case 1:
                //报告问题
                let assistantsVC = XS_MineAssistantsVC()
                navigationController?.pushViewController(assistantsVC, animated: true)
                break
//            case 3:
            case 2:
                //关于
                let aboutVC = XS_AboutVC()
                navigationController?.pushViewController(aboutVC, animated: true)
                break
            default:
                break
            }
            break
        default:
            break
        }
    }
    
}
